<!DOCTYPE html>












  




<html class="theme-next gemini use-motion" lang="zh-CN">
<head>
  <!-- hexo-inject:begin --><!-- hexo-inject:end --><meta charset="UTF-8"/>
<meta name="google-site-verification" content="o9IkI77-fxkhBZW-n0ww9JALMCqdDbeTgdcXO_Bw4Zc" />
<meta name="baidu-site-verification" content="3frqY9KiVO" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2"/>
<meta name="theme-color" content="#222">



  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">










<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />



















  
  
  
  

  
    
    
  

  
    
      
    

    
  

  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Monda:300,300italic,400,400italic,700,700italic|Roboto Slab:300,300italic,400,400italic,700,700italic|Lobster Two:300,300italic,400,400italic,700,700italic|PT Mono:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.4.1" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/logo.png?v=6.4.1">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/logo.png?v=6.4.1">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/logo.png?v=6.4.1">


  <link rel="mask-icon" href="/images/logo.svg?v=6.4.1" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '6.4.1',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="摘要：本文写作的初衷源于基于HMM模型序列标注的一个实验，在实验完成之后，如果迫切想知道采用的序列标注模型好坏，有哪些指标可以度量。于是，就产生了对这一专题进度的学习总结，这样也便于其他人参考，节约大家的时间。本文依旧旨在简明扼要地梳理出模型评估核心指标，关键以期达到实用的目的。本章首先介绍基于统计角度的模型评估，然后介绍模型评估的方法，最后对模型选择进行介绍。（本文原创，转载必须注明出处.）">
<meta name="keywords" content="准确率,召回率,F度量值,模型选择,模型评估">
<meta property="og:type" content="article">
<meta property="og:title" content="机器学习之模型评估">
<meta property="og:url" content="https://bainingchao.github.io/2019/02/13/机器学习之模型评估/index.html">
<meta property="og:site_name" content="白宁超的官网">
<meta property="og:description" content="摘要：本文写作的初衷源于基于HMM模型序列标注的一个实验，在实验完成之后，如果迫切想知道采用的序列标注模型好坏，有哪些指标可以度量。于是，就产生了对这一专题进度的学习总结，这样也便于其他人参考，节约大家的时间。本文依旧旨在简明扼要地梳理出模型评估核心指标，关键以期达到实用的目的。本章首先介绍基于统计角度的模型评估，然后介绍模型评估的方法，最后对模型选择进行介绍。（本文原创，转载必须注明出处.）">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170308169-1292852263.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170308169-1292852263.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170322513-951472577.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170340341-1583763592.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170439044-830539412.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170457841-1145620499.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170510482-1650016951.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170516919-836002879.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170522810-2042241123.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170631966-2146545362.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170641107-687141274.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170703029-1638906262.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170712122-857673859.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170720497-1651256328.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170734388-1701286946.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170849576-73700769.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170953685-1502804122.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718171003372-2106997227.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718171016154-1333145983.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718171029372-78838970.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718171040529-1949888983.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101410341-914028706.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101511372-1915794539.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101654841-775463264.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101732060-581925635.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101755857-1756734763.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101815794-1920529673.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101827951-594261296.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101836763-1192934474.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101849372-2103249073.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101900544-649129756.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101906935-775120886.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101915919-162492745.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101915919-162492745.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719102027154-1584845635.png">
<meta property="og:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719102035451-93411190.png">
<meta property="og:image" content="http://pub.idqqimg.com/wpa/images/group.png">
<meta property="og:updated_time" content="2019-02-13T09:41:05.322Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="机器学习之模型评估">
<meta name="twitter:description" content="摘要：本文写作的初衷源于基于HMM模型序列标注的一个实验，在实验完成之后，如果迫切想知道采用的序列标注模型好坏，有哪些指标可以度量。于是，就产生了对这一专题进度的学习总结，这样也便于其他人参考，节约大家的时间。本文依旧旨在简明扼要地梳理出模型评估核心指标，关键以期达到实用的目的。本章首先介绍基于统计角度的模型评估，然后介绍模型评估的方法，最后对模型选择进行介绍。（本文原创，转载必须注明出处.）">
<meta name="twitter:image" content="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170308169-1292852263.png">



  <link rel="alternate" href="/atom.xml" title="白宁超的官网" type="application/atom+xml" />




  <link rel="canonical" href="https://bainingchao.github.io/2019/02/13/机器学习之模型评估/"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>机器学习之模型评估 | 白宁超的官网</title>
  









  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript><!-- hexo-inject:begin --><!-- hexo-inject:end -->

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <!-- hexo-inject:begin --><!-- hexo-inject:end --><div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

	<!-- <a href="https://github.com/bainingchao"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub"></a> !-->
	
    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">白宁超的官网</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <h1 class="site-subtitle" itemprop="description">专注人工智能领域研究</h1>
      
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-首页">
    <a href="/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-标签">
    <a href="/tags/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />标签</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-分类">
    <a href="/categories/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-th"></i> <br />分类</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-归档">
    <a href="/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-视频">
    <a href="/videos/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br />视频</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-书籍">
    <a href="/books/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-th"></i> <br />书籍</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-链接">
    <a href="/links/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />链接</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-关于">
    <a href="/about/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于</a>
  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />搜索</a>
        </li>
      
    </ul>
  

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://bainingchao.github.io/2019/02/13/机器学习之模型评估/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="白宁超">
      <meta itemprop="description" content="本站主要研究深度学习、机器学习、自然语言处理等前沿技术。ML&NLP交流群：436303759 <span><a target="_blank" href="http://shang.qq.com/wpa/qunwpa?idkey=ef3bbb679b06ac59b136c57ba9e7935ff9d3b10faeabde6e4efcafe523bbbf4d"><img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="自然语言处理和机器学习技术QQ交流：436303759 " title="自然语言处理和机器学习技术交流"></a></span>">
      <meta itemprop="image" content="/../images/header.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="白宁超的官网">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">机器学习之模型评估
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-02-13 17:31:53 / 修改时间：17:41:05" itemprop="dateCreated datePublished" datetime="2019-02-13T17:31:53+08:00">2019-02-13</time>
            

            
              

              
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/机器学习/" itemprop="url" rel="index"><span itemprop="name">机器学习</span></a></span>

                
                
                  ，
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/机器学习/模型评估/" itemprop="url" rel="index"><span itemprop="name">模型评估</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="post-meta-item-icon"
            >
            <i class="fa fa-eye"></i>
             阅读次数： 
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
            </span>
          
		  

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p>摘要：本文写作的初衷源于基于HMM模型序列标注的一个实验，在实验完成之后，如果迫切想知道采用的序列标注模型好坏，有哪些指标可以度量。于是，就产生了对这一专题进度的学习总结，这样也便于其他人参考，节约大家的时间。本文依旧旨在简明扼要地梳理出模型评估核心指标，关键以期达到实用的目的。本章首先介绍基于统计角度的模型评估，然后介绍模型评估的方法，最后对模型选择进行介绍。（本文原创，转载必须注明出处.）</p>
</blockquote>
<a id="more"></a>
<h1 id="统计角度介绍模型概念"><a href="#统计角度介绍模型概念" class="headerlink" title="统计角度介绍模型概念"></a>统计角度介绍模型概念</h1><h2 id="算法模型"><a href="#算法模型" class="headerlink" title="算法模型"></a>算法模型</h2><blockquote>
<p>概念简述</p>
</blockquote>
<p>李航《统计学习方法》一书：统计学习方法是由模型、策略和算法构成的，即统计学习方法的三要素构成，简化：方法=模型+策略+算法</p>
<p>维基百科对数学模型描述：数学模型是对所描述的对象用数学语言所作出的描述和处理。</p>
<p>百度百科对策略描述：策略式学习是一项复杂的智能活动，学习过程与推理过程是紧密相连的。按照学习中使用推理的多少，机器学习所采用的策略大体上可分为4种——机械学习、通过传授学习、类比学习和通过事例学习。学习中所用的推理越多，系统的能力越强。</p>
<p>单从定义看，不免让人一头雾水，究竟何为模型？还是没有明确的概念。下文将以数学描述以及形式化阐述来解决这个问题。首先解决了什么是模型，咱们才能进行模型好坏指标的评价，进而选择适合的学习模型。</p>
<p>模型：所有学习的条件概率分布或者决策函数。</p>
<p>模型的假设空间：包含所有有可能的条件概率分布或者决策函数。</p>
<blockquote>
<p>实例解析</p>
</blockquote>
<p>假设决策函数是输入变量的线性函数，模型的假设空间就是这些线性函数构成的函数集合，假设空间中的模型一般为无穷多个。【现实应用：假设解决序列词性标注的的函数模型M，模型假设空间中由不同参数构成的M模型。（不是很严谨，辅助理解。）】</p>
<p>形式化表示：假设空间F表示，假设空间可以为决策函数的集合；</p>
<p>x，y在输入空间X和输出空间Y上的变量，这时F通常由一个参数向量决定的函数族表示；</p>
<p>参数向量θ取值于n维欧氏空间 ，称为参数空间。</p>
<p>假设空间也可以定义为条件概率集合：</p>
<p>其中x和y是定义在输入空间X和Y上的随机变量，这时F通常是一个参数向量决定的条件概率分布族；</p>
<p>参数向量θ取值于n维欧氏空间，称为参数空间。</p>
<p> 注意：由决策函数表示的模型为非概率模型（如上述F函数），由条件概率表示的模型为概率模型（如上述y=f(x)函数）。</p>
<h2 id="模型评估和模型选择"><a href="#模型评估和模型选择" class="headerlink" title="模型评估和模型选择"></a>模型评估和模型选择</h2><blockquote>
<p> 训练误差和测试误差</p>
</blockquote>
<p>好的模型的特征：对已知数据和未知数据都有很好的预测能力。</p>
<p>学习方法评估标准：基于损失函数的模型训练误差和测试误差为指标。</p>
<p>假设学习到的模型<img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170308169-1292852263.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170308169-1292852263.png">&nbsp;,训练误差是模型&nbsp;<img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170308169-1292852263.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170308169-1292852263.png">关于训练数据集的平均损失：&lt;/p&gt;</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">&nbsp;<img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170322513-951472577.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170322513-951472577.png"></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">其中N是训练样本容量。</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">测试误差是模型关于测试数据集的平均损失：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">&nbsp;<img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170340341-1583763592.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170340341-1583763592.png"></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">其中N’是训练样本容量。</p><p></p>
<blockquote>
<p> 实例</p>
</blockquote>
<p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">若损失函数是0-1损失的时候（0-1损失参考具体相关知识），测试误差就变成了常见的测试数据集上的误差率。</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170439044-830539412.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170439044-830539412.png">，这里的I是指示函数，即<img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170457841-1145620499.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170457841-1145620499.png">&nbsp;时为1，否则为0。</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">相应的，常见的测试数据集上的准确率是：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170510482-1650016951.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170510482-1650016951.png">，这里的I是指示函数，即&nbsp;<img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170516919-836002879.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170516919-836002879.png">时为1，否则为0。</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">显然：&nbsp;<img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170522810-2042241123.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170522810-2042241123.png">&nbsp;</p>

<blockquote>
<p>实例解析</p>
</blockquote>
<p>根据误差率和准确率可知，测试误差反映了学习方法对未知测试数据集的预测能力。测试误差小的方法具有很好的预测能力，能更有效地预测。通常将学习方法对未知数据的预测能力称为泛化能力。</p>
<p>由此我们可以应用到现实的NLP模型中。诸如分类模型，当测试误差更小的时候，分类更加准确；聚类模型中，当测试误差较小时候，聚类效果更好等等。因此，我们为了追求较小的测试误差，就要在训练上下功夫。但是，一味苛求训练效果好、训练误差小，以至于所选择的模型复杂度非常高，这样的低训练误差就一定能换回好的预测吗？事实上这就容易出现过拟合现象，如何避免过拟合选择更好的模型？下节继续。</p>
<h2 id="过拟合与欠拟合的模型选择"><a href="#过拟合与欠拟合的模型选择" class="headerlink" title="过拟合与欠拟合的模型选择"></a>过拟合与欠拟合的模型选择</h2><blockquote>
<p>何时进行模型选择？</p>
</blockquote>
<p>当假设空间含有不同复杂度（如：不同的参数个数）的模型时，就要面临模型选择问题，以期我们表达出的模型与真实的模型（参数个数）相同或相近。</p>
<p>过拟合：一味追求提高对训练数据的预测能力，所选择模型的复杂度往往比真实模型高，此现象就是过拟合。</p>
<p>过拟合指学习时选择的模型包含的参数过多，以至于出现模型对现有数据预测得好，但是对未知数据预测能力较差。模型选择准则是在避免过拟合的前提下尽可能地去提高模型预测的能力。</p>
<blockquote>
<p>实例</p>
</blockquote>
<p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">给出一个训练数据集&nbsp;<img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170631966-2146545362.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170631966-2146545362.png">&nbsp;和一个M次多项式的模型</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170641107-687141274.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170641107-687141274.png">&nbsp;</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">完成下面10个数据点的拟合。</p>

<p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170703029-1638906262.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170703029-1638906262.png">是输入序列集x的观察值</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170712122-857673859.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170712122-857673859.png">是输出序列集y的观察值</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">M 次项式是解决问题的模型</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">W为参数</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">解决如上问题按照经验风险最小化策略求解参数即可，即数学表达：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">&nbsp;<img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170720497-1651256328.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170720497-1651256328.png"></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">损失函数为平方损失，1/2是便于计算。然后将模型公式和训练数据代入风险最小化公式：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170734388-1701286946.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170734388-1701286946.png">，最后采用最小二乘法（过程略）求解。</p>

<blockquote>
<p>实例分析</p>
</blockquote>
<p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">如上图给出M=0,1,3,9的多项式函数的拟合情况。当M=0时，多项式变成了一个常数，数据拟合表现很差；当M=1时，多项式曲线为一条直线，拟合依旧差；当M=9时，多项式通过每一个点，训练误差0。从训练数据拟合角度分析，M=9时效果最好，但是训练数据本身存在很多噪音，对未来数据预测能力差，达不到预期的效果。这就是过拟合，虽然训练数据表现好但是未知数据预测差。当M=3时，多项式曲线对训练数据拟合效果比较好，对未知数据拟合也很好，其模型也简单，可以选择。总结：模型选择时，不仅仅考虑对已知数据的预测能力，还有考虑对未知数据的预测能力。</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">训练误差和测试误差与模型复杂度的关系如下图13-1所示：</p>

<center>

![](https://i.imgur.com/ME9FyuT.jpg)

图13-1 训练误差和测试误差与模型复杂度对比图

</center>


<p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">可知，当模型复杂度增大的时候，训练误差会逐渐减小并趋近于0，而测试误差会先减小到最小值后又反向增大。当选择模型复杂度过大时，过拟合问题就会出现。</p>

<blockquote>
<p> 补充</p>
</blockquote>
<p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">以下提供NLP序列句子识别标注实例，用以更好理解本节。（以下实例以帮助读者理解为旨要，假设可能不是特别严谨）</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">训练数据集T={(南 B),(海 I),(是 I), (中 I),(国 I),(领 I), (土 I),(。 O) }未知数据P={(不 B), (容 I),(争I),( 议 I),（。 O）}采用BIO标注，B代表句子开始，I代表中间连续词，O代表句子结束。假设采用模型M识别, M次多项式的模型</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170849576-73700769.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170849576-73700769.png">&nbsp;</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">完成下面句子识别。&nbsp;</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><strong>结果分析：</strong></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">M=0时候，模型一个常数效果很差，识别如下：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170953685-1502804122.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718170953685-1502804122.png"></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">M=1时候，模型一条直线效果很差，识别如下：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718171003372-2106997227.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718171003372-2106997227.png"></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">M=3时候，模型曲线拟合基本合理，且未知数据预测较好，识别如下：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718171016154-1333145983.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718171016154-1333145983.png"></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">M=9时候，模型一条直线效果很差，识别如下：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718171029372-78838970.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718171029372-78838970.png"></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">训练数据集：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718171040529-1949888983.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160718171040529-1949888983.png"></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><strong>实验可知</strong>：左侧为训练模型的数据，右侧为测试模型的数据。当M=0时，训练误差和测试误差都很大；当M=1时，训练误差和测试误差较大；当M=3时，训练误差比M=9的训练误差大，总体训练误差还好，但是预测误差却小于M=9时的预测误差。综合比较，选择M=3的模型效果会更好。</p>

<h1 id="模型评估与选择"><a href="#模型评估与选择" class="headerlink" title="模型评估与选择"></a>模型评估与选择</h1><h2 id="模型评估的概念"><a href="#模型评估的概念" class="headerlink" title="模型评估的概念"></a>模型评估的概念</h2><p>评估准确率的常用技术：保持和随机子抽样、K-折交叉验证、自助方法</p>
<p>统计显著性检验：评估模型准确率</p>
<p>ROC曲线：接收者操作特征曲线比较分类器效果好坏</p>
<h2 id="模型评估的评测指标"><a href="#模型评估的评测指标" class="headerlink" title="模型评估的评测指标"></a>模型评估的评测指标</h2><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><strong>混淆矩阵</strong>：正元组和负元组的合计。详细如表13-1所示：</p>
<center>

表13-1 混淆矩阵表

![](https://i.imgur.com/RuIzvaI.png)

</center>




<strong>评估度量</strong>：（其中P:正样本数； N：负样本数； TP：真正例 ；TN：真负例 ；FP：假正例； FN：假负例）评估信息度量如表13-2所示。

<center>

表13-2 评估信息度量表

<img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101410341-914028706.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101410341-914028706.png">

</center>






<p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><span style="color: rgb(255, 0, 0);">注意：学习器的准确率最好在检验集上估计，检验集由训练集模型未使用的含有标记的数据构成。</span></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">各参数描述如下：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">TP（真正例/真阳性）：是指被学习器正确学习的正元组，令TP为真正例的个数。</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">TN（真负例/真阴性）：是指被学习器正确学习的负元组，令TN为真负例的个数。</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">FP（假正例/假阳性）：是被错误的标记为正元组的负元组，令FP为假正例的个数。</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">FN（假负例/假阴性）：是被错误的标记为负元组的正元组，令FN为假负例的个数。</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><strong>高准确率的学习模型</strong>：大部分元组应该在混合矩阵的对角线上，而其他为0或者接近0，即FP和FN为0。其本质上是一个对角矩阵时准确率最高。</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><strong>准确率</strong>：正确识别的元组所占的比例。又叫做识别率，公式如下：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">&nbsp;<strong><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101511372-1915794539.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101511372-1915794539.png"></strong></p>
## 词性标注为例分析模型评估


词性标注为例分析模型评估详细信息如表13-3所示：
<center>

表13-3 词性标注为例分析模型评估表

<img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101654841-775463264.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101654841-775463264.png">

</center>

<strong>错误率：</strong>错误识别元组所占的比例，又叫误识别率，公式如下：<p></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101732060-581925635.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101732060-581925635.png">&nbsp;&nbsp;或者1-accuracy(M)</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">检验时，应采用检验集未加入训练集的数据。当采用训练集估计模型时，会再带入误差，这种称为乐观估计。准确率可以度量正确标注的百分比，但是不能正确度量错误率。诸如样本不平衡时，即负样本稀疏的时候。比如：欺诈、癌症等，这种情况下应使用灵敏性特效性度量。</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><strong>灵敏度又叫真正识别率</strong>：正确识别的正元组的百分比，公式如下：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101755857-1756734763.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101755857-1756734763.png"></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><strong>特效性又叫真负例率</strong>：正确识别的正元组的百分比，公式如下：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101815794-1920529673.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101815794-1920529673.png"></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><strong>准确率的灵敏度和特效性的函数关系</strong>：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101827951-594261296.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101827951-594261296.png"></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><strong>精度</strong>：精确性的度量即标记为正元组实际为正元组的百分比，公式如下：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">、<img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101836763-1192934474.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101836763-1192934474.png"></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><strong>召回率</strong>：完全性的度量即正元组标记为正的百分比，公式如下：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101849372-2103249073.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101849372-2103249073.png"></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">精度和召回率之间趋向于逆关系，有可能出现一个指标降低另一个指标提升的情况。此刻两个指标预想达到综合引出了F度量值</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><strong>F度量</strong>（又叫F分数）：用精度和召回率的方法把他们组合到一个度量中。公式如下：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">&nbsp;<img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101900544-649129756.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101900544-649129756.png"></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">&nbsp;<img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101906935-775120886.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101906935-775120886.png"></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><strong>比较</strong>：F度量是精度和召回率的调和均值，赋予精度和召回率相等的权重，&nbsp;<img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101915919-162492745.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101915919-162492745.png">度量是精度和召回率加权度量，它赋予召回率权重是精度的β倍。诸如中文词汇中常用词的权重比生僻词的权重大，而这也符合实际应用。</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><span style="color: rgb(255, 0, 0);">注意：当元组属于多个类时，不适合使用准确率。当数据均衡分布即正负元组基本相当时，准确率效果最好，而召回率、特效性、精度、F和&nbsp;<img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101915919-162492745.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719101915919-162492745.png">更适合于样本分布不均的情况。</span></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><strong></strong></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><em></em></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"></p>
## 模型评估的几种方法

模型评估包括多种方法，常见的评估流程如图13-2所示。

<center>
![](https://i.imgur.com/CRWjZyh.png)

图13-2 模型评估流程图
</center>

1）随机二次抽样评估准确率：是保持方法的一种变形，将保持方法重复K次，总准确率估计是每次迭代准确率的平均值<p></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">2）K-折交叉验证评估准确率：（建议10折）</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">K-折交叉验证：将初始的数据随机分为大小大致相同的K份，训练和检验进行K次，如第1次迭代第一份数据作为检查集，其余K-1份作为训练集，第2次迭代。第二份数据作为检验集，其余K-1份作为训练集，以此类推直到第K份数据作为检验集为止。此方法每份样本用于训练的次数一致且每份样本只作为一次检验集。准确率是K次迭代正确元组总数除以初始数据元组总数。一般建议采用10-折交叉验证估计准确率，因为它的偏移和方差较低。</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">3）自助法评估准确率：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">自助法有放回的均匀抽样，常用632自助法。即63.2%原数据将出现在自助样本中。而其余38.8%元数据形成检验集。</p>

<h1 id="ROC曲线比较学习器模型"><a href="#ROC曲线比较学习器模型" class="headerlink" title="ROC曲线比较学习器模型"></a>ROC曲线比较学习器模型</h1><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><strong>成本效益</strong>（风险增益）：如错误的预测癌症患者没有患病比将没有患病的病人归类癌症的代价大等等事件，据此给予不同的权重。</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">ROC曲线又叫接受者操作特征曲线，比较两个学习器模型的可视化工具，横坐标参数假正例率，纵坐标参数是真正例率。以此汇聚成的曲线，越靠近对角线（随机猜测线）模型越不好。</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><strong>真正例率</strong>（召回率）：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719102027154-1584845635.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719102027154-1584845635.png"></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><strong>假正例率</strong>：</p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><img src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719102035451-93411190.png" alt="" style="border: 0px;" _src="http://images2015.cnblogs.com/blog/380252/201607/380252-20160719102035451-93411190.png"></p><p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;"><strong>实例解析</strong>：以10个检验元组的概率分类器为例绘制ROC曲线。数据如表13-4所示</p>

<center>
表13-4 检验元组数据表

![](https://i.imgur.com/9TJKhbX.png)

</center>


<p style="line-height: 24px; margin: 10px auto; font-size: 15px; white-space: normal;">由以上数据绘制ROC曲线如图13-3所示：</p>

<center>

![](https://i.imgur.com/JjoM0mI.png)

图13-3 ROC曲线图
</center>




<p>由此可知，对角线为随机猜测线，模型的ROC曲线越靠近对角线，模型的准确率越低。如果很好的模型，真正比例比较多，曲线应是陡峭的从0开始上升，后来遇到真正比例越来越少，假正比例元组越来越多，曲线平缓变的更加水平。完全正确的模型面积为1。&lt;/p&gt;</p>
<hr>
<h1 id="参考文献"><a href="#参考文献" class="headerlink" title="参考文献"></a>参考文献</h1><ol>
<li><a href="https://github.com/BaiNingchao/MachineLearning-1" target="_blank" rel="noopener">GitHub</a></li>
<li><a href="https://baike.baidu.com/item/%E8%87%AA%E7%84%B6%E8%AF%AD%E8%A8%80%E5%A4%84%E7%90%86%E7%90%86%E8%AE%BA%E4%B8%8E%E5%AE%9E%E6%88%98" target="_blank" rel="noopener">图书：《自然语言处理理论与实战》</a></li>
</ol>
<h1 id="完整代码下载"><a href="#完整代码下载" class="headerlink" title="完整代码下载"></a>完整代码下载</h1><blockquote>
<p>源码请进【机器学习和自然语言QQ群：436303759】文件下载：<a target="_blank" href="http://shang.qq.com/wpa/qunwpa?idkey=ef3bbb679b06ac59b136c57ba9e7935ff9d3b10faeabde6e4efcafe523bbbf4d"><img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="自然语言处理和机器学习技术QQ交流" title="自然语言处理和机器学习技术交流"></a></p>
</blockquote>
<h1 id="作者声明"><a href="#作者声明" class="headerlink" title="作者声明"></a>作者声明</h1><blockquote>
<p>本文版权归作者所有，旨在技术交流使用。未经作者同意禁止转载，转载后需在文章页面明显位置给出原文连接，否则相关责任自行承担。</p>
</blockquote>

      
    </div>

    

    
    
    

    
      <div>
        <div id="wechat_subscriber" style="display: block; padding: 10px 0; margin: 20px auto; width: 100%; text-align: center">
    <img id="wechat_subscriber_qcode" src="/uploads/wechat.png" alt="白宁超 wechat" style="width: 200px; max-width: 100%;"/>
    <div>扫一扫关注微信公众号，机器学习和自然语言处理，订阅号datathinks！</div>
</div>

      </div>
    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div></div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.jpg" alt="白宁超 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpg" alt="白宁超 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/准确率/" rel="tag"><i class="fa fa-tag"></i> 准确率</a>
          
            <a href="/tags/召回率/" rel="tag"><i class="fa fa-tag"></i> 召回率</a>
          
            <a href="/tags/F度量值/" rel="tag"><i class="fa fa-tag"></i> F度量值</a>
          
            <a href="/tags/模型选择/" rel="tag"><i class="fa fa-tag"></i> 模型选择</a>
          
            <a href="/tags/模型评估/" rel="tag"><i class="fa fa-tag"></i> 模型评估</a>
          
        </div>
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div class="social_share">
            
               <div>
                 
  <div class="bdsharebuttonbox">
    <a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
    <a href="#" class="bds_douban" data-cmd="douban" title="分享到豆瓣网"></a>
    <a href="#" class="bds_sqq" data-cmd="sqq" title="分享到QQ好友"></a>
    <a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间"></a>
    <a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信"></a>
    <a href="#" class="bds_tieba" data-cmd="tieba" title="分享到百度贴吧"></a>
    <a href="#" class="bds_twi" data-cmd="twi" title="分享到Twitter"></a>
    <a href="#" class="bds_fbook" data-cmd="fbook" title="分享到Facebook"></a>
    <a href="#" class="bds_more" data-cmd="more"></a>
    <a class="bds_count" data-cmd="count"></a>
  </div>
  <script>
    window._bd_share_config = {
      "common": {
        "bdText": "",
        "bdMini": "2",
        "bdMiniList": false,
        "bdPic": ""
      },
      "share": {
        "bdSize": "16",
        "bdStyle": "0"
      },
      "image": {
        "viewList": ["tsina", "douban", "sqq", "qzone", "weixin", "twi", "fbook"],
        "viewText": "分享到：",
        "viewSize": "16"
      }
    }
  </script>

<script>
  with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='/static/api/js/share.js?cdnversion='+~(-new Date()/36e5)];
</script>

               </div>
            
            
               <div id="needsharebutton-postbottom">
                 <span class="btn">
                    <i class="fa fa-share-alt" aria-hidden="true"></i>
                 </span>
               </div>
            
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/02/13/命名实体识别技术/" rel="next" title="命名实体识别技术">
                <i class="fa fa-chevron-left"></i> 命名实体识别技术
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/02/13/漫谈条件随机场/" rel="prev" title="漫谈条件随机场">
                漫谈条件随机场 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zOTc5NC8xNjMyMQ=="></div>
    </div>

  
 





        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/../images/header.png"
                alt="白宁超" />
            
              <p class="site-author-name" itemprop="name">白宁超</p>
              <p class="site-description motion-element" itemprop="description">本站主要研究深度学习、机器学习、自然语言处理等前沿技术。ML&NLP交流群：436303759 <span><a target="_blank" href="http://shang.qq.com/wpa/qunwpa?idkey=ef3bbb679b06ac59b136c57ba9e7935ff9d3b10faeabde6e4efcafe523bbbf4d"><img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="自然语言处理和机器学习技术QQ交流：436303759 " title="自然语言处理和机器学习技术交流"></a></span></p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives">
                
                    <span class="site-state-item-count">65</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">29</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">119</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  <a href="https://github.com/bainingchao" target="_blank" title="GitHub" rel="external nofollow"><i class="fa fa-fw fa-github"></i>GitHub</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://www.google.com.hk/" target="_blank" title="Google" rel="external nofollow"><i class="fa fa-fw fa-google"></i>Google</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://www.baidu.com/" target="_blank" title="百度" rel="external nofollow"><i class="fa fa-fw fa-globe"></i>百度</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://weibo.com/p/1005056002073632?is_all=1" target="_blank" title="微博" rel="external nofollow"><i class="fa fa-fw fa-weibo"></i>微博</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://www.cnblogs.com/baiboy/" target="_blank" title="博客园" rel="external nofollow"><i class="fa fa-fw fa-globe"></i>博客园</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://mp.weixin.qq.com/s/s97I4gtEJIt5rMivWMkPkQ" target="_blank" title="微信公众号" rel="external nofollow"><i class="fa fa-fw fa-weixin"></i>微信公众号</a>
                  
                </span>
              
            </div>
          

          
          

          
          

          
            
          
          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#统计角度介绍模型概念"><span class="nav-number">1.</span> <span class="nav-text">统计角度介绍模型概念</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#算法模型"><span class="nav-number">1.1.</span> <span class="nav-text">算法模型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#模型评估和模型选择"><span class="nav-number">1.2.</span> <span class="nav-text">模型评估和模型选择</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#过拟合与欠拟合的模型选择"><span class="nav-number">1.3.</span> <span class="nav-text">过拟合与欠拟合的模型选择</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#模型评估与选择"><span class="nav-number">2.</span> <span class="nav-text">模型评估与选择</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#模型评估的概念"><span class="nav-number">2.1.</span> <span class="nav-text">模型评估的概念</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#模型评估的评测指标"><span class="nav-number">2.2.</span> <span class="nav-text">模型评估的评测指标</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#ROC曲线比较学习器模型"><span class="nav-number">3.</span> <span class="nav-text">ROC曲线比较学习器模型</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#参考文献"><span class="nav-number">4.</span> <span class="nav-text">参考文献</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#完整代码下载"><span class="nav-number">5.</span> <span class="nav-text">完整代码下载</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#作者声明"><span class="nav-number">6.</span> <span class="nav-text">作者声明</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>

<div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">白宁超</span>

  

  
</div>




  



  <!--<div class="powered-by">由 <a class="theme-link" target="_blank" rel="external nofollow" href="https://hexo.io">Hexo</a> 强力驱动 v3.7.1</div> -->



   <!--<span class="post-meta-divider">|</span>-->



   <!--<div class="theme-info">主题 – <a class="theme-link" target="_blank" rel="external nofollow" href="https://theme-next.org">NexT.Gemini</a> v6.4.1</div>-->




        <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<div class="busuanzi-count">
  
    <span class="site-uv" title="总访客量">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
    </span>
  

  
    <span class="site-pv" title="总访问量">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
    </span>
  
</div>









        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
	
    

    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.4.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.4.1"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=6.4.1"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=6.4.1"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.4.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.4.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.4.1"></script>



  



  
    <script type="text/javascript">
      window.livereOptions = {
        refer: '2019/02/13/机器学习之模型评估/'
      };
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  










  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  

  
  

  
    
      <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      },
      TeX: {equationNumbers: { autoNumber: "AMS" }}
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>
<script type="text/javascript" src="//cdn.jsdelivr.net/npm/mathjax@2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

    
  


  
  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "box";
      
          pbOptions.boxForm = "horizontal";
      
          pbOptions.position = "bottomCenter";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Linkedin,Facebook";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
  </script>

  

  

  

  

  

  

  <!-- 页面点击小红心 -->
	<script type="text/javascript" src="../js/src/love.js"></script><!-- hexo-inject:begin --><!-- Begin: Injected MathJax -->
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({"tex2jax":{"inlineMath":[["$","$"],["\\(","\\)"]],"skipTags":["script","noscript","style","textarea","pre","code"],"processEscapes":true},"TeX":{"equationNumbers":{"autoNumber":"AMS"}}});
</script>

<script type="text/x-mathjax-config">
  MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
      all[i].SourceElement().parentNode.className += ' has-jax';
    }
  });
</script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js">
</script>
<!-- End: Injected MathJax -->
<!-- hexo-inject:end -->
</body>
</html>
